package com.nowcoder.community.controller.elastic;

import com.nowcoder.community.entity.DiscussPost;
import com.nowcoder.community.entity.Page;
import com.nowcoder.community.service.ElasticSearchService;
import com.nowcoder.community.service.LikeService;
import com.nowcoder.community.service.UserService;
import com.nowcoder.community.util.CommunityConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchPage;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(path = "/elastic")
public class SearchController implements CommunityConstant {

    @Autowired
    private ElasticSearchService elasticSearchService;

    @Autowired
    private UserService userService;

    @Autowired
    private LikeService likeService;

    @RequestMapping(path = "/search", method = RequestMethod.GET)
    public String searchByElasticEngine(String keyword, Page page, Model model) {
        // 搜索帖子
        // current - 1的原因: Page对象default的current值为1
        // 而ElasticSearch中, page的current是从0开始的
        SearchPage<DiscussPost> searchResults = elasticSearchService.searchDiscussPost(keyword, page.getCurrent() - 1, page.getLimit());

        // 聚合数据
        List<Map<String, Object>> discussPosts = new ArrayList<>();
        if(searchResults != null && !searchResults.isEmpty()) {
            for(SearchHit<DiscussPost> hit : searchResults) {
                DiscussPost post = hit.getContent();

                // 高亮搜素结果中的keyword
                // 包括title和content
                List<String> titleHighLightedField = hit.getHighlightField("title");
                if(!titleHighLightedField.isEmpty()) {
                    post.setTitle(titleHighLightedField.get(0));
                }
                List<String> contentHighLightedField = hit.getHighlightField("content");
                if(!contentHighLightedField.isEmpty()) {
                    post.setContent(contentHighLightedField.get(0));
                }

                Map<String, Object> map = new HashMap<>();
                map.put("DiscussPost", post);
                map.put("User", userService.findUserById(post.getUserId()));
                map.put("Likes", likeService.findEntityLikeCount(ENTITY_TYPE_POST, post.getId()));

                discussPosts.add(map);
            }
        }

        model.addAttribute("discussPosts", discussPosts);
        // 方便前端显示用户输入的关键词
        model.addAttribute("keyword", keyword);

        // 设置分页信息
        page.setPath("/elastic/search?keyword=" + keyword);
        page.setRows(searchResults != null ? (int)searchResults.getTotalElements() : 0);

        return "/site/search";
    }

}
